package com.team2.insulatingtools.modular.tools.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team2.insulatingtools.constants.ToolsStatus;
import com.team2.insulatingtools.modular.tools.entity.Scrap;
import com.team2.insulatingtools.modular.tools.entity.ToolDetail;
import com.team2.insulatingtools.modular.tools.entity.Tools;
import com.team2.insulatingtools.modular.tools.mapper.ScrapMapper;
import com.team2.insulatingtools.modular.tools.mapper.ToolDetailMapper;
import com.team2.insulatingtools.modular.tools.pojo.request.ScrapRequest;
import com.team2.insulatingtools.modular.tools.pojo.response.ScrapHistoryResponse;
import com.team2.insulatingtools.modular.tools.service.ScrapService;
import com.team2.insulatingtools.modular.tools.service.ToolsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: insulating_tools
 * @ClassName ScrapServiceImpl
 * @description:
 * @author: 张宁
 * @create: 2023-04-07 16:09
 * @Version 1.0
 **/
@Service
public class ScrapServiceImpl extends ServiceImpl<ScrapMapper, Scrap> implements ScrapService {
    @Resource
    private ScrapMapper mapper;

    @Resource
    private ToolDetailMapper toolDetailMapper;

    @Resource
    private ToolsService toolsService;


    @Override
    public List<Scrap> findList(ScrapRequest scrapRequest) {
        LambdaQueryWrapper<Scrap> wrapper = this.createWrapper(scrapRequest);
        return this.list(wrapper);
    }


    @Override
    public List<Scrap> findListWithSup(ScrapRequest scrapRequest) {
        scrapRequest.setStatue(3);
        LambdaQueryWrapper<Scrap> wrapper = this.createWrapper(scrapRequest);
        return this.list(wrapper);
    }


    @Override
    public List<Scrap> all() {
        return mapper.selectList(null);
    }

    @Override
    public List<Scrap> queryByWorkShop(String workshop) {

        QueryWrapper<Scrap> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(workshop), "workshop_name", workshop);
        return mapper.selectList(wrapper);

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(ScrapRequest scrapRequest) {
        Integer id = scrapRequest.getId();
        Scrap scrap = mapper.selectScrapById(id);
        int insert = mapper.insert(scrap);
        //工具历史数据
        Tools tools = toolsService.getBaseMapper()
                .selectOne(new QueryWrapper<Tools>().eq("id", scrap.getToolid()));
        toolsService.addToolLog(scrap.getToolid(), tools.getSpecification(), tools.getKeeper(),
                ToolsStatus.报废未补充.getValue());
        return insert;
    }

    @Override
    public int updateStatue(ScrapRequest scrapRequest) {
        Integer id = scrapRequest.getId();
        Scrap scrap = new Scrap();
        scrap.setId(id);
        scrap.setStatue(ToolsStatus.报废未补充.getValue());
        return mapper.updateById(scrap);
    }

    @Override
    public List<ScrapHistoryResponse> findHistoryList(ScrapRequest scrapRequest) {
        Integer id = scrapRequest.getToolid();
        QueryWrapper<ToolDetail> wrapper = new QueryWrapper<>();
        List<ToolDetail> toolDetails = toolDetailMapper.selectList(wrapper);
        List<ScrapHistoryResponse> scrapHistoryResponses = new ArrayList<>();
        for (ToolDetail t : toolDetails) {
            ScrapHistoryResponse s = new ScrapHistoryResponse();
            s.setTestDate(t.getTestDate());
            s.setExpireDate(t.getExpireDate());
            s.setTester(t.getTester());
            s.setAuditor(t.getAuditor());
            s.setOperateDate(t.getOperateDate());
            s.setOperateConclusion(t.getOperateConclusion());
            scrapHistoryResponses.add(s);
        }
        return scrapHistoryResponses;
    }


    private LambdaQueryWrapper<Scrap> createWrapper(ScrapRequest scrapRequest) {
        LambdaQueryWrapper<Scrap> queryWrapper = new LambdaQueryWrapper<>();
//        String workshop = scrapRequest.getWorkshop();
        if (scrapRequest.getOrgName().indexOf("车间") > -1) {
            String workshop = scrapRequest.getOrgName();
            queryWrapper.eq(ObjectUtil.isNotEmpty(workshop), Scrap::getWorkshop, workshop);
        } else if (scrapRequest.getOrgName().indexOf("工区") > -1 || scrapRequest.getOrgName().indexOf("组") > -1) {
            String team = scrapRequest.getTeam();
            queryWrapper.eq(ObjectUtil.isNotEmpty(team), Scrap::getTeam, team);
        }
        Integer statue = scrapRequest.getStatue();

        queryWrapper.eq(ObjectUtil.isNotNull(statue), Scrap::getStatue, statue);
        return queryWrapper;
    }
}
